const util = require('../../../../utils/util.js');
const api = require('../../../../config/api.js');
const user = require('../../../../services/user.js');

var app = getApp();

Page({
  data: {
    tmCrReqEntitys: [],
    seleckedIndex: -1
  },
  onLoad: function (options) {
    // 页面初始化 options为页面跳转所带来的参数

  },
  onReady: function () {
    // 页面渲染完成

  },
  onShow: function () {
    // 页面显示
    this.getIndexData();
  },
  onHide: function () {
    // 页面隐藏

  },
  onUnload: function () {
    // 页面关闭

  },
  getIndexData: function () {
    // 获取用户的登录信息
    let token = wx.getStorageSync('token');

    if (token) {
      let that = this;

      util.request(api.ReagentRevertlist).then(function (res) {
        if (res.code === 0) {
          that.setData({
            tmCrReqEntitys: res.tmCrReqEntitys
          });
        }
      });
    } else {
      wx.navigateTo({
        url: "/pages/cr/userCenter/login/login"
      });
    }
  },
  checkedItem: function (event) {
    let itemIndex = event.target.dataset.itemIndex;
    let that = this;

    let tmCrReqEntitys = this.data.tmCrReqEntitys.map(function (element, index, array) {
      // 选中下标，标记为选中状态，其他为非选中状态
      if (itemIndex == index) {
        element.checked = true;
      } else {
        element.checked = false;
      }
      
      return element;
    });

    that.setData({
      tmCrReqEntitys: tmCrReqEntitys,
      seleckedIndex: itemIndex
    });
  },
  checkoutOrder: function () {
    //获取已选择的商品
    let that = this;

    if (this.data.seleckedIndex <= -1) {
      util.showErrorToast('请选择归还信息');
      return false;
    }
    // 获取选中归还试剂申请
    let tmCrReqEntity = this.data.tmCrReqEntitys[this.data.seleckedIndex];
    // 转换成json格式
    let dataString = encodeURIComponent(JSON.stringify(tmCrReqEntity));

    wx.navigateTo({
      url: '../reagentRevertDetail/reagentRevertDetail?dataString=' + dataString
    })
  }
})